//RTK
import {
  createAsyncThunk,
  createSlice,
  type PayloadAction,
} from "@reduxjs/toolkit";
// 自己的
import { coffeeType } from "@/type/coffeeType";
import { getCoffeeLists } from "@/services/coffee";

const initialState: {
  coffeeLists: coffeeType[];
} = {
  coffeeLists: [], //咖啡列表
};

export const fetchCoffeeDataAction: any = createAsyncThunk(
  "fetchData",
  (param, { dispatch }) => {
    getCoffeeLists().then((res) => {
      dispatch(changeCoffeeListsAction(res));
    });
  }
);

const coffeeSlice = createSlice({
  name: "coffee",
  initialState,
  reducers: {
    changeCoffeeListsAction(state, { payload }: PayloadAction<coffeeType[]>) {
      state.coffeeLists = payload;
    },
  },
});

export const { changeCoffeeListsAction } = coffeeSlice.actions;
export default coffeeSlice.reducer;
